package com.bbs.detai1713.conf;

import com.bbs.detai1713.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class DBWebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    DataService dataService;

    @Bean
    PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(dataService);
    }

    @Override
    public void configure(WebSecurity web) {
        //将项目中静态资源路径开放出来
        web.ignoring().antMatchers("/config/**", "/css/**", "/fonts/**", "/img/**", "/js/**", "/webjars/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/register").permitAll()
                .antMatchers("/selectBoard").permitAll()
                .antMatchers("/selectPosting").permitAll()
                .antMatchers("/logout").authenticated()
                .antMatchers("/logout/success").permitAll()
                .antMatchers("/login/failure").permitAll()
                .antMatchers("/isLogin").permitAll()
                .antMatchers("/selectReply").permitAll()
                .antMatchers("/selectPostingByPostingId").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("https://www.zjy9951.site/bbs/#/loginAuth")
                .loginProcessingUrl("/login")
                .successForwardUrl("/login/success")
                .failureForwardUrl("/login/failure")
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/logout/success")
                .clearAuthentication(true)
                .invalidateHttpSession(true)
                .deleteCookies("JSESSIONID")
                .and()
                .csrf().disable();
    }

    @Bean
    RoleHierarchy roleHierarchy() {
        RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
        String hierarchy = "ROLE_1 > ROLE_2 > ROLE_3";
        roleHierarchy.setHierarchy(hierarchy);
        return roleHierarchy;
    }
}
